package com.ellricky.baselib.ui.order

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import androidx.recyclerview.widget.RecyclerView
import com.andr.common.ui.activity.BaseToolBarActivity
import com.andr.common.ui.fragment.BaseFragment
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.widget.refresh.RvRefreshListener
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.R
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.dialog.picker.EPickerUtil
import com.ellricky.baselib.entity.BaseData
import com.ellricky.baselib.entity.IdNamePickerEntity
import com.ellricky.baselib.entity.OrderInfo
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.*
import kotlinx.android.synthetic.main.activity_order_manage.*
import java.util.*
import kotlin.collections.HashMap

class OrderManagerFragment : BaseFragment<Void, BasePresenter<Void>>(), RvRefreshListener,
    AdapterView.OnItemClickListener {

    private var paramMap: HashMap<String, String> = hashMapOf()
    private var dataSource: List<OrderInfo> = arrayListOf()
    private var refresh = false
    private var ePickerUtil: EPickerUtil? = null
    private var empEid = LocalCacheUtil.getUid()
    private var confirmState = ""
    private var checkState = ""

    private var month: String = TimeUtils.date2String(Date(), TimeUtils.FORMAT_DIVER_MONTH)

    private val stateList by lazy {
        val list = arrayListOf<IdNamePickerEntity>();
        list.add(IdNamePickerEntity("", "全部"))
        list.add(IdNamePickerEntity("0", "未确认"))
        list.add(IdNamePickerEntity("1", "已确认"))
        list.add(IdNamePickerEntity("2", "确认未通过"))
        list
    }

    private val checkList by lazy {
        val list = arrayListOf<IdNamePickerEntity>();
        list.add(IdNamePickerEntity("", "全部"))
        list.add(IdNamePickerEntity("0", "未审核"))
        list.add(IdNamePickerEntity("1", "已审核"))
        list.add(IdNamePickerEntity("2", "审核未通过"))
        list
    }

    private val adapter by lazy {
        OrderAdapter(requireActivity())
    }

    private var isShowBackIcon: Boolean = false

    companion object {
        fun newInstance(extras: Bundle?): OrderManagerFragment {
            val fragment = OrderManagerFragment()
            fragment.arguments = extras
            return fragment
        }
    }

    override fun getContentLayout(): Int {
        return R.layout.activity_order_manage
    }

    override fun initUIView(p0: View?) {
        tv_title.text = "订单管理"

        rvView.init(this)
        ePickerUtil = EPickerUtil(requireActivity())

        isShowBackIcon = arguments?.getBoolean(BaseConstant.KEY_SHOW_BACK) ?: false

        if (!isShowBackIcon) {
            ib_back.visibility = View.VISIBLE
        }

        ib_back.setOnClickListener {
            activity?.finish()
        }

        if (LocalCacheUtil.isPresidentRole()) {
            //选择人员
            tv_teacher?.setOnClickListener {
                ePickerUtil?.checkStaffTypePicker(null) { _, entry ->
                    tv_teacher.text = entry.name
                    empEid = entry.id
                    loadDataOnInit()
                }
            }
        } else {
            tv_teacher.text = LocalCacheUtil.readLocalUser().U_Name
            empEid = LocalCacheUtil.readLocalUser().id.toString()
        }

        //选择确认状态
        tv_confirm_status?.setOnClickListener {
            ePickerUtil?.showOrderStatePicker(stateList, null) { _, entry ->
                tv_confirm_status.text = entry.name
                confirmState = entry.id
                loadDataOnInit()
            }
        }

        //选择时间
        tv_date?.setOnClickListener {
            ePickerUtil?.showMonthMnPicker("") { content,
                                                 _,
                                                 _ ->
                tv_date.text = TimeUtils.convertFormat(
                    content,
                    TimeUtils.FORMAT_DIVER_MONTH,
                    TimeUtils.FORMAT_MONTH
                )
                month = content ?: ""
                loadDataOnInit()
            }
        }

        tv_check_status?.setOnClickListener {
            ePickerUtil?.showCheckStatePicker(checkList, null) { _, entry ->
                tv_confirm_status.text = entry.name
                checkState = entry.id
                loadDataOnInit()
            }
        }

        tv_date.text = TimeUtils.date2String(Date(), TimeUtils.FORMAT_MONTH)

        loadDataOnInit()
    }

    private fun loadDataOnInit() {
        rvView.loadContent(true)
    }

    private fun initParamMap() {
        paramMap["action"] = "memberLogList"
        paramMap["eid"] = LocalCacheUtil.getUid()
        paramMap["school_id"] = LocalCacheUtil.getSchoolId()
        paramMap["page"] = rvView.curPage.toString()
        paramMap["page_size"] = "40"
        paramMap["emp_eid"] = empEid
        paramMap["month"] = month
        paramMap["check_state"] = checkState
        paramMap["confirm_state"] = confirmState
    }

    override fun setAdapter(rv: RecyclerView?) {
        val adapter = adapter
        adapter.setOnItemClickListener(this)
        rv?.adapter = adapter
    }

    override fun loadData(refresh: Boolean) {
        this.refresh = refresh
        val url = EAppUtil.getApiHost()
        initParamMap()
        val callback: NewEBaseCallback = getResponseCallback2()
        HttpUtils.getInstance().postFrom(url, paramMap, callback)
    }

    private fun getResponseCallback2(): NewEBaseCallback {
        return object : NewEListCallBack2<OrderInfo>(requireActivity()) {
            override fun onSuccess(
                data: MutableList<OrderInfo>,
                baseData: BaseData<OrderInfo>,
                msg: String?
            ) {
                dataSource = data
                rvView.onLoadFinish(data, refresh)
                tv_order_number.text = "订单笔数：${baseData.allcount}笔"
            }
        }
    }

    override fun updateAdapterData(refresh: Boolean) {
        if (refresh) adapter.refresh(dataSource)
        else adapter.loadMore(dataSource)
    }

    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        jump<OrderDetailActivity> {
            val bean = adapter.get(position)
            putExtra("id", bean.log_id)
        }
    }
}